home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_06 / saks / inttst3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-08  |  1.2 KB  |  82 lines

  1. Listing 10 - A test program for a generic queue of void * used as a 
  2. queue of int
  3.  
  4. //
  5. // inttst3.cpp - test genq3
  6. //
  7.  
  8. #include <iostream.h>
  9.  
  10. #include "genq3.h"
  11. #include "showheap.h"
  12.  
  13. #define DIM(a) (sizeof(a)/sizeof(a[0]))
  14.  
  15. void print_int(void *e)
  16.     {
  17.     cout << " " << *(int *)e;
  18.     }
  19.  
  20. void test()
  21.     {
  22.     char c;
  23.     size_t qn;
  24.     int qe;
  25.     int *pqe;
  26.     void *pv;
  27.     genq q[4];
  28.     while (cin >> c)
  29.         {
  30.         showheap();
  31.         if (c == 'q')
  32.             break;
  33.         if (c == 'a')
  34.             {
  35.             cin >> qn >> qe;
  36.             if (qn >= DIM(q))
  37.                 cout << "no such queue\n";
  38.             else
  39.                 q[qn].append(new int(qe));
  40.             }
  41.         else if (c == 'c')
  42.             {
  43.             cin >> qn;
  44.             if (qn >= DIM(q))
  45.                 cout << "no such queue\n";
  46.             else
  47.                 q[qn].clear();
  48.             }
  49.         else if (c == 'r')
  50.             {
  51.             cin >> qn;
  52.             if (qn >= DIM(q))
  53.                 cout << "no such queue\n";
  54.             else if (q[qn].remove(pv))
  55.                 {
  56.                 pqe = (int *)pv;
  57.                 cout << "removed " << *pqe << '\n';
  58.                 delete pqe;
  59.                 }
  60.             else
  61.                 cout << "q[" << qn << "] is empty\n";
  62.             }
  63.         else
  64.             continue;
  65.         for (size_t i = 0; i < DIM(q); ++i)
  66.             {
  67.             cout << i << ':';
  68.             q[i].apply(print_int);
  69.             cout << "\n";
  70.             }
  71.         }
  72.     }
  73.  
  74. int main()
  75.     {
  76.     showheap();
  77.     test();
  78.     showheap();
  79.     return 0;
  80.     }
  81.  
  82.